package com.mark.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mark.database.Mark1;
import com.mark.database.Mark2;
import com.mark.toolImpl.AddMarkImpl;
import com.mark.toolImpl.GetMarkImpl;

import net.sf.json.JSONObject;

public class AddMarkServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		JSONObject message=new JSONObject();
		if(session.getAttribute("uId")!=null){
			int jobid=Integer.valueOf(request.getParameter("jobId"));
			String PayType=request.getParameter("PayType");
			int uId=Integer.valueOf(String.valueOf(session.getAttribute("uId")));
			if(PayType.equals("日结")){
				GetMarkImpl get=new GetMarkImpl();
				Mark1 m1=new Mark1();
				m1.setJobId(jobid);
				m1.setUid(uId);
				if(!get.getMark1(m1)){
					AddMarkImpl add=new AddMarkImpl();
					m1.setJobId(jobid);
					m1.setUid(uId);
					if(add.addMark1(m1)!=-1){
						message.put("message", "收藏成功!");
					}
					else{
						message.put("message", "收藏失败!");
					}
				}
				else{
					message.put("message", "此工作已收藏，请不要重复该操作。!");
				}
			}
			else{
				GetMarkImpl get=new GetMarkImpl();
				Mark2 m2=new Mark2();
				m2.setJobId(jobid);
				m2.setUid(uId);
				if(!get.getMark2(m2)){
					AddMarkImpl add=new AddMarkImpl();
					m2.setJobId(jobid);
					m2.setUid(uId);
					if(add.addMark2(m2)!=-1){
						message.put("message", "收藏成功!");
					}
					else{
						message.put("message", "收藏失败!");
					}
				}
				else{
					message.put("message", "此工作已收藏，请不要重复该操作。!");
				}
			}
		}
		else{
			message.put("message", "请先登录！");
		}
		response.getWriter().print(message);  
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}



}
